<template>
  <!-- 工具 侧边栏 -->
  <div class="sidebar">
    <!-- 开关 -->
    <div class="switch" @click="router.replace('/')"><span></span></div>
    <!-- 工具标签 -->
    <div class="sidebar-wrap">
      <div class="sidebar-nav">
        <div @click="checkedPreviewTool(index, item)" v-for="(item, index) in props.sideBarList" :key="index" :class="[
          'nav',
          previewTool !== undefined && index == previewTool ? 'checked' : '',
        ]">
          {{ item.text }}
        </div>
      </div>
    </div>
  </div>
</template>
 
<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
const router = useRouter();
const props = defineProps(['sideBarList', 'routerBase'])

const previewTool = ref(undefined);     // 当前预览组件
function checkedPreviewTool(index, item) {
  if (previewTool.value === index) return
  previewTool.value = index;
  router.replace({ path: props.routerBase + item.path })
}
</script>

<style lang="scss" scoped>
.sidebar {
  position: fixed;
  top: 0;
  right: 0;

  .switch {
    cursor: pointer;
    position: absolute;
    top: 20px;
    right: 20px;
    width: 40px;
    height: 40px;
    border: 1px solid var(--border-hui);
    border-radius: 10px;
    transition: all 0.4s;
    z-index: 2;

    &>span {
      display: block;
      width: 30px;
      height: 4px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      background: #dadada;
      border-radius: 2px;
      transition: all 0.4s;
    }

    &::before {
      content: "";
      display: block;
      width: 30px;
      height: 4px;
      position: absolute;
      top: 25%;
      left: 5px;
      transform: translateY(-50%);
      background: #dadada;
      border-radius: 2px;
      transition: all 0.4s;
      transform-origin: left;
    }

    &::after {
      content: "";
      display: block;
      width: 30px;
      height: 4px;
      position: absolute;
      top: 75%;
      left: 5px;
      transform: translateY(-50%);
      background: #dadada;
      border-radius: 2px;
      transition: all 0.4s;
      transform-origin: left;
    }

    &:hover {
      transform: translateY(-10%);
      box-shadow: 1px 4px 5px #dadadab0;
      background: rgb(206, 206, 206);

      &>span {
        opacity: 0;
      }

      &::after {
        width: calc(1.2 * 30px);
        transform: translateY(-50%) rotate(-33.6deg);
        background: #ffffff;
      }

      &::before {
        width: calc(1.2 * 30px);
        transform: translateY(-50%) rotate(33.6deg);
        background: #ffffff;
      }
    }

    &:active {
      transform: translateY(-10%) scale(0.94);
    }
  }

  .sidebar-wrap {
    --background-color: #0084ff3d;

    width: 140px;
    height: 75vh;
    display: flex;
    justify-content: flex-end;
    overflow-y: scroll;
    position: absolute;
    right: 0;
    top: 80px;

    &::-webkit-scrollbar {
      display: none;
    }

    .sidebar-nav {
      min-width: 100px;
      font-size: 14px;

      .nav {
        min-width: 100px;
        padding: 0 10px;
        height: 40px;
        box-sizing: border-box;
        border-radius: 10px 0 0 10px;
        border: 1px solid #d8d8d893;
        border-right: none;
        display: flex;
        align-items: center;
        justify-content: center;
        text-align: center;
        transition: all 0.2s;
        cursor: pointer;
        transform-origin: right;
        margin: 10px 0;

        &:hover {
          background: var(--background-color);
          border-color: #d8d7d7;
          transform: scale(1.1);
        }

        &:active {
          transform: scale(0.94);
        }
      }

      .checked {
        background: var(--background-color);
      }
    }
  }
}
</style>